package com.ist.viewpagerhelper;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.ist.viewpagerhelper.adpter.FpagerAdapter;
import com.ist.viewpagerhelper.adpter.GlidePagerAdapter;
import com.ist.viewpagerhelper.anim.DepthPageTransformer;
import com.ist.viewpagerhelper.anim.ZoomOutPageTransformer;
import com.ist.viewpagerhelper.view.BannerViewPagerTail;
import com.ist.viewpagerhelper.view.LineIndecator;
import com.ist.viewpagerhelper.view.BannerViewPager;
import com.ist.viewpagerhelper.indicator.TransIndicator;

import java.util.List;

/**
 * 该类为 viewpager 的工具类，用于导航栏、首页轮播加载或者试图切换等作用
 * Created by Administrator on 2017/10/19.
 */

public class ViewPagerHelper {
    private static final String TAG = "zsr";
    public static final int GLIDE = 1;
    public static final int LOOPER = 2;
    public static final int FRAGMENT = 3;
    /**
     * others
     */
    private Context mContext;
    private boolean isGlide = false;
    private BannerViewPager mLoopViewPager;
    private BannerViewPagerTail mBannerViewPager;


    public ViewPagerHelper(final Builder builder){
        ViewPager viewPager = builder.viewPager;

        mContext = builder.context;
        // 首次引导图
        if (builder.mode == GLIDE) {
            GlidePagerAdapter adapter = new GlidePagerAdapter(builder.views);
            viewPager.setAdapter(adapter);
            View view = builder.linearLayout;
           /* // 底部的小圆点的实现
            if (view instanceof ScaleIndicator) {
                ScaleIndicator circleIndicator = (ScaleIndicator) builder.linearLayout;
                circleIndicator.setViewPager(builder);
            }*/
            if (view instanceof TransIndicator){
                TransIndicator transIndicator = (TransIndicator) builder.linearLayout;
                transIndicator.setViewPager(viewPager,builder.startView);
            }
        }

        // 轮播控件
        if (builder.mode == LOOPER){
            if (builder.views != null){
                mLoopViewPager = (BannerViewPager) builder.viewPager;
               // mLoopViewPager.setDataViews(builder.views);
            }else {
                if (builder.sources != null){
                    mBannerViewPager = (BannerViewPagerTail) builder.viewPager;
                    //mBannerViewPager.setBannerDatas(builder.sources,builder.sourceType);
                }
            }

            View view = builder.linearLayout;
            // 底部的小圆点的实现
           /* if (view instanceof ScaleIndicator) {
                ScaleIndicator circleIndicator = (ScaleIndicator) builder.linearLayout;
                circleIndicator.setViewPager(builder);
            }
            if (view instanceof TransIndicator){
                TransIndicator transIndicator = (TransIndicator) builder.linearLayout;
                transIndicator.setViewPager(viewPager,builder.startView);
            }*/
        }

        // viewpager + fragment 模式

        if (builder.mode == FRAGMENT){
            FpagerAdapter adapter = new FpagerAdapter(builder.views,builder.titles);
            viewPager.setAdapter(adapter);

            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    LineIndecator lineIndecator = (LineIndecator) builder.indecator;
                    lineIndecator.onScroll(position,positionOffset);
                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }

        if (builder.zoomTransFormer){
            viewPager.setPageTransformer(true,new ZoomOutPageTransformer());
        }
        if (builder.depthTransFormer){
            viewPager.setPageTransformer(true, new DepthPageTransformer());
        }

    }






    public static class Builder{
        public Context context;
        public ViewPager viewPager;
        public View linearLayout;
        public List<View> views;
        public int selector;
        public View startView;
        public boolean usecircle;
        public long autoTime;
        public List<String> titles;
        public int mode;
        public View indecator;
        public boolean depthTransFormer;
        public boolean zoomTransFormer;
        public Object[] sources;
        public int sourceType;
        public Builder setContext(Context context){
            this.context = context;
            return this;
        }
        public Builder setViewPager(ViewPager viewPager){
            this.viewPager = viewPager;
            return this;
        }
        public Builder setLinearLayout(View linearLayout){
            this.linearLayout = linearLayout;
            return this;
        }

        public Builder setStartView(View view){
            this.startView = view;
            return  this;
        }

        public Builder setUseDefaultCircle(boolean usecircle){
            this.usecircle = usecircle;
            return this;
        }


        public Builder setDataViews(List<View> views){
            this.views = views;
            return this;
        }
        public Builder setDataObject(Object[] sources){
            this.sources = sources;
            return this;
        }
        public Builder setSourceType(int sourceType){
            this.sourceType = sourceType;
            return this;
        }

        public Builder setSelector(int selector){
            this.selector = selector;
            return this;
        }



        public Builder setAutoTime(long autoTime){
            this.autoTime = autoTime;
            return this;
        }

        public Builder setTitles(List<String> titles){
            this.titles = titles;
            return this;
        }

        public Builder setViewPagerMode(int mode){
            this.mode = mode;
            return this;
        }

        public Builder setIndecator(View indecatorview){
            this.indecator = indecatorview;
            return this;
        }

        public Builder useDepthTransFormer(boolean depthTransFormer){
            this.depthTransFormer = depthTransFormer;
            return this;
        }

        public Builder useZoomTransFormer(boolean zoomTransFormer){
            this.zoomTransFormer = zoomTransFormer;
            return this;
        }

        public ViewPagerHelper builder(){
            return new ViewPagerHelper(this);
        }
    }

    public void onPause(){
        mLoopViewPager.onPause();
    }
    public void onResume(){
        mLoopViewPager.onReusme();
    }

}
